package ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.functions.jeval;

import java.util.List;

import net.sourceforge.jeval.Evaluator;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.LogicalFunction;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.EvaluationException;

public class JevalLogicalFunctionImpl implements LogicalFunction {

	private String functionString;

	private Evaluator evaluator;

	public JevalLogicalFunctionImpl(String functionString) {
		this.functionString = functionString;
		this.evaluator = new Evaluator();
	}

	public Boolean evaluate(List<Boolean> values) throws EvaluationException {
		this.evaluator.setVariableResolver(new JevalVariableResolverImpl(values));
		try {
			return this.evaluator.getBooleanResult(this.functionString);
		} catch (net.sourceforge.jeval.EvaluationException e) {
			throw new EvaluationException("An error occurred while evaluating the function: " + this.functionString
					+ "\n" + e.getMessage());
		}
	}

	public Object clone() throws CloneNotSupportedException {
		return new JevalLogicalFunctionImpl(new String(functionString));
	}

}
